﻿using PacketRush.Services.Commanding;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PacketRush.Core;

namespace PacketRush.Services.Commanding.Test
{
    
    
    /// <summary>
    ///This is a test class for ServerCommandServiceTest and is intended
    ///to contain all ServerCommandServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ServerCommandServiceTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for RemoveCommand
        ///</summary>
        [TestMethod()]
        public void TestRemoveCommand()
        {
            ServerCommandService target = new ServerCommandService(); 
            Command command = new TestCommand();
            target.AddCommand(command);

            // Finding command should not return null
            Assert.IsNotNull(target.FindCommand(command.CommandName));

            // Finding a command is case insensitive, should not return null
            Assert.IsNotNull(target.FindCommand(command.CommandName.ToUpper()));

            // Lets try remove this command
            target.RemoveCommand(command.CommandName);

            // The command is now removed, so finding the command should return null
            Assert.IsNull(target.FindCommand(command.CommandName));
        }

        /// <summary>
        ///A test for FindCommand
        ///</summary>
        [TestMethod()]
        public void FindCommandTest()
        {
            ServerCommandService target = new ServerCommandService(); 
            Command command = new TestCommand();
            target.AddCommand(command);

            // Finding command should not return null
            Assert.IsNotNull(target.FindCommand(command.CommandName));

            // Finding a command is case insensitive, should not return null
            Assert.IsNotNull(target.FindCommand(command.CommandName.ToUpper()));
        }

        /// <summary>
        ///A test for ExecuteCommand
        ///</summary>
        [TestMethod()]
        public void ExecuteCommandTest1()
        {
            
            ServerCommandService target = new ServerCommandService(); 
            Command command = new TestCommand();
            command.Execute += new System.EventHandler<CommandEventArgs>(ExecuteCommandTest_Execute);
            target.AddCommand(command);

            Assert.IsTrue(target.ExecuteCommand("testcommand -o \"foo, bar, bat\" -x 50.55 -y 50%"));
        }

        bool ExecuteCommandTest_ExecuteCalled = false;
        void ExecuteCommandTest_Execute(object sender, CommandEventArgs e)
        {
            Assert.AreEqual(e.Command.CommandName, new TestCommand().CommandName);
            Assert.AreEqual(e.Args.Length, 6);
            ExecuteCommandTest_ExecuteCalled = true;
        }

        /// <summary>
        ///A test for ExecuteCommand
        ///</summary>
        [TestMethod()]
        public void ExecuteCommandTest()
        {
            ServerCommandService target = new ServerCommandService(); 
            Command command = new TestCommand();
            command.Execute += new System.EventHandler<CommandEventArgs>(ExecuteCommandTest_Execute);
            target.AddCommand(command);

            Assert.IsTrue(target.ExecuteCommand("testcommand", new string[] { "-o", "foo, bar, bat", "-x", "50.55", "-y", "50%" }));
        }

        /// <summary>
        ///A test for DeliverPacket
        ///</summary>
        [TestMethod()]
        public void DeliverPacketTest()
        {
            ExecuteCommandTest_ExecuteCalled = false;

            ServerCommandService target = new ServerCommandService(); 
            Command command = new TestCommand();
            command.Execute += new System.EventHandler<CommandEventArgs>(ExecuteCommandTest_Execute);
            target.AddCommand(command);
            
            // construct a packet
            PacketWriter writer = new PacketWriter();
            writer.Write("testcommand -o \"foo, bar, bat\" -x 50.55 -y 50%", System.Text.Encoding.UTF8);
            Packet packet = writer.ToPacket();

            target.DeliverPacket(packet);

            Assert.IsTrue(ExecuteCommandTest_ExecuteCalled);
        }

        /// <summary>
        ///A test for AddCommand
        ///</summary>
        [TestMethod()]
        public void AddCommandTest()
        {
            ServerCommandService target = new ServerCommandService(); 
            Command command = new TestCommand();
            target.AddCommand(command);

            // Finding command should not return null
            Assert.IsNotNull(target.FindCommand(command.CommandName));

            // Finding a command is case insensitive, should not return null
            Assert.IsNotNull(target.FindCommand(command.CommandName.ToUpper()));
        }
    }
}
